﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class ApiKey : BaseEntity
	{
		#region Properties

		public String Hashcode { get; set; }
		public DateTime LoginDate { get; set; }
		public Boolean FullAccess { get; set; }
		public User User { get; set; }

		#endregion

		#region Constructor

		public ApiKey()
		{
			User = new User();
		}

		public ApiKey(String text)
		{
			Construct(DataAccess.ApiKey_Get(text));
		}

		public ApiKey(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Loading

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "ApiKeyId");
			Hashcode = DataHandler.GetString(reader, "ApiKeyText");
			LoginDate = DataHandler.GetDateTime(reader, "ApiKeyLastLogin");
			FullAccess = DataHandler.GetBoolean(reader, "ApiKeyFullAccess");
			Active = DataHandler.GetBoolean(reader, "ApiKeyActive");

			User = new User();
			User.Id = DataHandler.GetInt32(reader, "UserId");
			User.Email = DataHandler.GetString(reader, "UserEmail");

			if (!String.IsNullOrEmpty(User.Email))
			{
				User.Load(reader);
			}
		}

		#endregion
	}
}